package com.qqtech.qquan.homemaking.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.model.HomemakingSysDict;
import com.qqtech.qquan.homemaking.model.HomemakingSysDictQuery;
import com.qqtech.qquan.homemaking.model.HomemakingSysDictVo;
import com.qqtech.qquan.homemaking.service.HomemakingSysDictService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/homemaking/sys/dict")
public class HomemakingSysDictController extends BaseAdminController {

	@Resource
	private HomemakingSysDictService homemakingSysDictService;

	@Override
	protected BaseService<HomemakingSysDict> getBaseService() {
		return homemakingSysDictService;
	}

	@RequestMapping(value = "/listDictType/{typeId}", method = RequestMethod.GET)
	public String listDictType(@PathVariable Integer typeId, Model model) {
		model.addAttribute("typeId", typeId);
		return "/homemaking/sys/dictType-list";
	}

	@RequestMapping(value = "/listDictType", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage listDictType(Integer typeId,
			HomemakingSysDictQuery query, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		if (query == null) {
			query = new HomemakingSysDictQuery();
		}
		query.setCode("type" + typeId.toString());
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.ASC, "id"));
		Page<HomemakingSysDictVo> page = homemakingSysDictService
				.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/addDictType/{typeId}", method = RequestMethod.GET)
	public String addDictType(@PathVariable Integer typeId, Model model) {
		model.addAttribute("typeId", typeId);
		return "/homemaking/sys/dictType-update";
	}

	/**
	 * 家政服务详情的 服务标准，服务流程 用homemakingSysDict保存，code保存分类id，name保存描述，remark保存图片路径
	 * 
	 * @param homemakingSysDictVo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addDictType", method = RequestMethod.POST)
	@ResponseBody
	public Result doAddDictType(HomemakingSysDictVo homemakingSysDictVo,
			HttpServletRequest request, HttpServletResponse response) {
		Result ret = new Result();
		if (homemakingSysDictVo == null) {
			ret.addErr("参数不能为空");
		} else {
			ret = check(homemakingSysDictVo,ret);
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			HomemakingSysDict entity = new HomemakingSysDict();
			entity.setType(homemakingSysDictVo.getType());
			if (homemakingSysDictVo.getType().equals("1")) {
				entity.setTypeDesc("服务标准");
			} else if (homemakingSysDictVo.getType().equals("2")) {
				entity.setTypeDesc("服务流程");
			} else if (homemakingSysDictVo.getType().equals("3")) {
				entity.setTypeDesc("服务说明");
			}
			entity.setNewAt(now);
			entity.setUpdAt(now);
			entity.setName(homemakingSysDictVo.getName());
			entity.setCode("type"+homemakingSysDictVo.getCode());
			entity.setRemark(homemakingSysDictVo.getRemark()==null?"":homemakingSysDictVo.getRemark());
			entity.setOidx(0);
			homemakingSysDictService.insert(entity);
		}
		return ret;
	}
	
	@RequestMapping(value = "/updateDictType/{id}", method = RequestMethod.GET)
	public String updateDictType(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
		HomemakingSysDictVo entity = homemakingSysDictService.getById(id);
		request.setAttribute("entity", entity);
		return "/homemaking/sys/dictType-update";
	}
	
	@RequestMapping(value = "/updateDictType", method = RequestMethod.POST)
	@ResponseBody
	public Result doUupdateDictType(HomemakingSysDictVo homemakingSysDictVo) {
		Result ret = new Result();
		if (homemakingSysDictVo == null || homemakingSysDictVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			ret = check(homemakingSysDictVo,ret);
		}
		if (ret.checkForm()) {
			HomemakingSysDict entity = new HomemakingSysDict();
			entity.setId(homemakingSysDictVo.getId());
			entity.setType(homemakingSysDictVo.getType());
			if (homemakingSysDictVo.getType().equals("1")) {
				entity.setTypeDesc("服务标准");
			} else if (homemakingSysDictVo.getType().equals("2")) {
				entity.setTypeDesc("服务流程");
			} else if (homemakingSysDictVo.getType().equals("3")) {
				entity.setTypeDesc("服务说明");
			}
			entity.setUpdAt(TimeUtil.now());
			entity.setName(homemakingSysDictVo.getName());
			entity.setCode(homemakingSysDictVo.getCode());
			entity.setRemark(homemakingSysDictVo.getRemark()==null?"":homemakingSysDictVo.getRemark());
			homemakingSysDictService.updateById(entity);
		}
		return ret;
	}
	
	public Result check(HomemakingSysDictVo homemakingSysDictVo, Result ret) {
		if (StringUtils.isBlank(homemakingSysDictVo.getType())) {
			ret.addErr("字典类型不能为空");
		}
		if (StringUtils.isBlank(homemakingSysDictVo.getCode())) {
			ret.addErr("家政服务分类id不能为空");
		}
		if (StringUtils.isBlank(homemakingSysDictVo.getName())) {
			ret.addErr("内容不能为空");
		}
		if (StringUtils.isNotBlank(homemakingSysDictVo.getRemark())
				&& StringUtils.isNotBlank(homemakingSysDictVo.getName())) {
			if (homemakingSysDictVo.getImgList() != null && homemakingSysDictVo.getNameList() != null) {
				if (homemakingSysDictVo.getImgList().size() != homemakingSysDictVo.getNameList().size()) {
					ret.addErr("上传的图片数量和内容的数量不一致");
				}
			}
		}
		return ret;
	}
	
	@RequestMapping(value = "/imgUpload/{key}", method = RequestMethod.GET)
	public String imgUpload(@PathVariable String key,Model model) {
		model.addAttribute("key", key);
		return "/homemaking/sys/dictType-imgUpload";
	}
}
